﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CTC
{
    /// <summary>
    /// 等待发送的包
    /// </summary>
    class DataManager
    {
        #region 属性
        /// <summary>
        /// 要发送的数据包，验证是否收到
        /// </summary>
        private List<DataPacket> SendPacket;
        /// <summary>
        /// 要发送的ACK数据
        /// </summary>
        private List<DataPacket> ACKPacket;
        #endregion

        #region 方法
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="reTime">重发时间，毫秒</param>
        public DataManager()
        {
            SendPacket = new List<DataPacket>();
            ACKPacket = new List<DataPacket>();
        }
        /// <summary>
        /// 添加要发送的数据
        /// </summary>
        /// <param name="dataPacket">数据</param>
        public void AddData(DataPacket dataPacket)
        {
            lock (SendPacket)
            {
                SendPacket.Add(dataPacket);
            }
        }
        /// <summary>
        /// 获取要发送的数据
        /// </summary>
        /// <returns></returns>
        public DataPacket GetData()
        {
            DataPacket result = null;
            lock (SendPacket)
            {
                if (SendPacket.Count != 0)
                {
                    result = SendPacket[0];
                    SendPacket.RemoveAt(0);
                }
            }
            return result;
        }
        /// <summary>
        /// 是否有数据
        /// </summary>
        /// <returns></returns>
        public bool HasData()
        {
            lock (SendPacket)
            {
                return SendPacket.Count > 0;
            }
        }
        /// <summary>
        /// 添加ACK
        /// </summary>
        /// <param name="dataPacket">数据</param>
        public void AddACKData(DataPacket dataPacket)
        {
            lock (ACKPacket)
            {
                ACKPacket.Add(dataPacket);
            }
        }
        /// <summary>
        /// 获取ACK
        /// </summary>
        /// <returns></returns>
        public DataPacket GetAckData()
        {
            DataPacket result = null;
            lock (ACKPacket)
            {
                if (ACKPacket.Count != 0)
                {
                    result = ACKPacket[0];
                    ACKPacket.RemoveAt(0);
                }
            }
            return result;
        }
        /// <summary>
        /// 是否有ACK
        /// </summary>
        /// <returns></returns>
        public bool HasAckData()
        {
            lock (ACKPacket)
            {
                return ACKPacket.Count > 0;
            }
        }
        /// <summary>
        /// 是否没有数据
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return !HasAckData() && !HasData();
        }
        #endregion
    }
}
